home *** CD-ROM | disk | FTP | other *** search
/ Screensavers 98 / Screensavers 98.iso / scr / pyro / fclasses.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-28  |  22.5 KB  |  706 lines

  1. #include "fclasses.h"
  2. #include "fix.h"
  3.  
  4. int PixelSize = 1;
  5.  
  6. // ############################################################################
  7. // Constructors
  8.  
  9. Flare::Flare (void)
  10. {
  11.     xPos          = yPos = xPlot = yPlot = ground = gravty = thrust = 0;
  12.     gndTerminated = alive = FALSE;
  13.     gravConst     = (1 * Fixed) >> (random (3)+1);// gravity constant... this case it's random.
  14.  
  15.     // trailer buffer settings. 
  16.     overBuf     = FALSE;
  17.     bufPnt      = -1;
  18.     trailLen    = 0;
  19.     trailer     = NULL;
  20.     colour      = RGB(255,255,255);
  21. }
  22.  
  23. Flare::Flare (int  x,           int      y,
  24.               int  thr,         int      grav,
  25.               int  cyc,         int      gnd,
  26.               int  trailLength, COLORREF col)
  27. {
  28.     init (x, y, thr, grav, cyc, gnd, trailLength, col);
  29. }
  30.  
  31. void Flare::init (int  x,           int      y,
  32.                   int  thr,         int     grav,
  33.                   int  cyc,         int      gnd,
  34.                   int  trailLength, COLORREF col)
  35. {
  36.     xPos   = x;
  37.     yPos   = y;
  38.     thrust = thr;
  39.     ground = gnd;              
  40.     cycles = cyc;
  41.     colour = col;
  42.     gravty = grav;
  43.  
  44.     // init internals ***** Change XPos, yPos and assign to xPlot, yPlot *****
  45.     xPlot  = yPlot = 0;
  46.     gndTerminated = FALSE;           // flare hasn't hit the ground!
  47.     alive         = TRUE;
  48.  
  49.     //**** Allocate trailer memory ****
  50.     // If memory is already allocated, but trail length has changed. Then
  51.     // delete/free memory.
  52.     if ((trailer != NULL) && (trailLen != trailLength))
  53.     {
  54.         delete trailer;
  55.         trailer = NULL;
  56.     }
  57.  
  58.     // Allocate memory
  59.     if (trailer == NULL)
  60.        trailer = new SaveXY[trailLength];
  61.  
  62.     // buffer variables for trailer
  63.     trailLen = trailLength;
  64.     overBuf  = FALSE;
  65.     bufPnt   = -1;
  66. }
  67.  
  68. void Flare::removePoint (HDC hdc, int x, int y)
  69. {
  70.     if (y > ground)
  71.        y = ground;
  72.  
  73.     switch (PixelSize)
  74.     {
  75.         case 1:
  76.              SetPixel(hdc, x, y,   0);
  77.              break;
  78.                              
  79.         case 2:
  80.              SetPixel (hdc, x, y, 0);
  81.              SetPixel (hdc, x, y+1, 0);
  82.              break;
  83.  
  84.         case 3:
  85.              SetPixel (hdc, x+1, y-1, 0);
  86.              SetPixel (hdc, x, y, 0);
  87.              SetPixel (hdc, x+1, y, 0);
  88.              break;
  89.                              
  90.         case 4:
  91.              SetPixel(hdc, x, y, 0);
  92.              SetPixel(hdc, x-1, y, 0);
  93.              SetPixel(hdc, x, y-1, 0);
  94.              SetPixel(hdc, x-1, y-1, 0);
  95.     }
  96. }
  97.  
  98. void Flare::showPoint (HDC hdc, int x, int y)
  99. {
  100.     if (y > ground)
  101.        y = ground;
  102.  
  103.     switch (PixelSize)
  104.     {
  105.         case 1:
  106.              SetPixel(hdc, x, y, colour);
  107.              break;
  108.                              
  109.         case 2:
  110.              SetPixel (hdc, x, y,   colour);
  111.              SetPixel (hdc, x, y+1, colour);
  112.              break;
  113.  
  114.         case 3:
  115.              SetPixel (hdc, x+1, y-1, colour);
  116.              SetPixel (hdc, x, y, colour);
  117.              SetPixel (hdc, x+1, y, colour);
  118.              break;
  119.                              
  120.         case 4:
  121.              SetPixel(hdc, x, y,     colour);
  122.              SetPixel(hdc, x-1, y,   colour);
  123.              SetPixel(hdc, x, y-1,   colour);
  124.              SetPixel(hdc, x-1, y-1, colour);
  125.     }
  126. }
  127.  
  128. BOOL Flare::move (HDC hdc)
  129. {
  130.     if (alive)// && (trailer != NULL))
  131.     {
  132.         // update flare
  133.         if (trailLen > 0)          
  134.         {
  135.             // decrement cycles of life for firework
  136.             cycles--;
  137.  
  138.             yPlot  = yPlot + gravty;      // decrease virtical lift
  139.             xPlot  = xPlot + thrust;      // decrease horizontal thrust  
  140.             gravty = gravty - gravConst;  // update gravity pull
  141.  
  142.             // **** Trailer maintenace ****
  143.             // load buffer from start
  144.             if (overBuf == FALSE)
  145.             {
  146.                 // If item exceeds buffer, rollover to start of buffer (queue system)
  147.                 if (bufPnt >= trailLen - 1)
  148.                 {
  149.                     bufPnt    = -1;
  150.                     overBuf   = TRUE;
  151.  
  152.                     removePoint (hdc, trailer [bufPnt+1].x, trailer [bufPnt+1].y);
  153.                 }
  154.  
  155.                 bufPnt++;
  156.                 trailer[bufPnt].x = xPos + (xPlot / Fixed);
  157.                 trailer[bufPnt].y = yPos - (yPlot / Fixed);
  158.             }
  159.             else // cycle thru queue buffer.
  160.             {
  161.                 if (cycles <= trailLen)
  162.                 {
  163.                     removePoint (hdc, trailer [trailLen-1].x, trailer [trailLen-1].y);
  164.                     trailLen--;
  165.                 }
  166.  
  167.                 if (bufPnt >= trailLen - 1)
  168.                    bufPnt = -1;
  169.  
  170.                 bufPnt++;
  171.                 removePoint (hdc, trailer [bufPnt].x, trailer [bufPnt].y);
  172.                 trailer [bufPnt].x = xPos + (xPlot / Fixed);
  173.                 trailer [bufPnt].y = yPos - (yPlot / Fixed);
  174.             }
  175.  
  176.             showPoint (hdc, trailer [bufPnt].x, trailer [bufPnt].y);
  177.         }
  178.         else
  179.         {
  180.             removePoint (hdc, trailer [0].x, trailer [0].y);
  181.             alive = FALSE;
  182.         }
  183.     }
  184.     return alive;
  185. }
  186.  
  187. // ############################################################################
  188. // Destructor
  189. Flare::~Flare(void)
  190. {
  191.     delete trailer;
  192. }
  193.  
  194.  
  195. // ############################################################################
  196. // ############################################################################
  197. // ############################################################################
  198.  
  199. void SkyRocket::init (int  starBNum,
  200.                       int  x,          int      y,
  201.                       int  thr,        int     grav,
  202.                       int  cyc,        int      gnd,
  203.                       int trailLength, COLORREF col)
  204. {
  205.     //**** Allocate explode memory ****
  206.     // If memory is already allocated, but explode number has changed. Then
  207.     // delete/free memory.
  208.     if ((pStarBurst != NULL) && (starBurstNum != starBNum))
  209.     {
  210.         delete pStarBurst;
  211.         pStarBurst = NULL;
  212.     }
  213.  
  214.     if (pStarBurst == NULL)
  215.        pStarBurst = new Flare[starBNum];
  216.  
  217.     // Initialise main rocket ....
  218.     mainRocket.init (x, y, thr, grav, cyc, gnd, trailLength, col);
  219.  
  220.     // Initialise explode variables.
  221.     explode      = FALSE;
  222.     starBurstNum = starBNum;
  223.     colour       = col;
  224.     trailLen     = trailLength;
  225.  
  226.     // Select colour item to change (i.e. increment!)
  227.     // test for single primary colours
  228.     const int incRocAmount = 3;
  229.           int testRGB = GetRValue(colour);
  230.               testRGB += GetGValue(colour);
  231.               testRGB += GetBValue(colour);
  232.  
  233.  
  234.     if (testRGB <= 256)          // Only one colour is active
  235.     {
  236.         // Pick an primary item that isn't set!
  237.         int pick = 0;
  238.         while (pick == 0)
  239.         {
  240.             colItemChange = random (3);
  241.             switch (colItemChange)
  242.             {
  243.                 case 0:
  244.                      if (GetRValue(colour) < 127)
  245.                         pick = -1;
  246.                      break;
  247.  
  248.                 case 1:
  249.                      if (GetGValue(colour) < 127)
  250.                         pick = -1;
  251.                      break;
  252.  
  253.                 case 2:
  254.                      if (GetBValue(colour) < 127)
  255.                         pick = -1;
  256.             }
  257.         } 
  258.     }
  259.     else
  260.         colItemChange = random(3);
  261.  
  262.     // Select the increment style according to current colour value.
  263.     switch (colItemChange)
  264.     {
  265.          // red
  266.          case 0:
  267.               if (GetRValue(colour) < 127)
  268.                  colChangeAmnt = incRocAmount;
  269.               else
  270.                  colChangeAmnt = -incRocAmount;
  271.               break;
  272.  
  273.          // green
  274.          case 1:
  275.               if (GetGValue(colour) < 127)
  276.                  colChangeAmnt = incRocAmount;
  277.               else
  278.                  colChangeAmnt = -incRocAmount;
  279.               break;
  280.  
  281.  
  282.          // blue
  283.          case 2:
  284.              if (GetBValue(colour) < 127)
  285.                 colChangeAmnt = incRocAmount;
  286.              else
  287.                 colChangeAmnt = -incRocAmount;
  288.              break;
  289.     }
  290. }
  291.  
  292. BOOL SkyRocket::move (HDC hdc)
  293. {
  294.     // If no memory allocated .... leave.
  295.     if (pStarBurst == NULL)
  296.        return FALSE;
  297.  
  298.     // Update main rocket, and then explode!
  299.     if (explode == FALSE)
  300.     {
  301.         if (mainRocket.move(hdc) == FALSE)
  302.         {
  303.             // Set up explosion when main rocket dies.
  304.             int x, y;
  305.             explode = TRUE;
  306.  
  307.             // intialise star burst.
  308.             mainRocket.getCurrentPos (x, y);
  309.  
  310.             // Implement random explosion type, two setup options exist.
  311.             int count;
  312.             switch (random(10))
  313.             {
  314.                 case 0:
  315.                      // Implement bridel veil.
  316.                      for (count = 0; count < starBurstNum; count++)
  317.                      {
  318.             
  319.                         pStarBurst[count].init (x,y,
  320.                                                 ((random (60)-30) * Fixed) >> random(2)+4, ((random (60)-20) * Fixed) >> random(2)+4,
  321.                                                 random(100), MAXY, random(trailLen)+5, colour);
  322.                      }
  323.                      break;
  324.  
  325.                 case 1:
  326.                      // Implement a super nova explosion.
  327.                      for (count = 0; count < starBurstNum; count++)
  328.                      {
  329.                           pStarBurst[count].init (x, y,
  330.                                                  ((random (60)-30) * Fixed) >> random(4), ((random (60)-30) * Fixed) >> random(4),
  331.                                                  random(100), MAXY, random(trailLen)+5, colour);
  332.                      }
  333.                      break;
  334.  
  335.                 case 2:
  336.                      // Circle small explosion.
  337.                      for (count = 0; count < starBurstNum; count++)
  338.                      {
  339.                         pStarBurst[count].init (x + random (10)-5,y + random (10)-5,
  340.                                                 ((random (30)-15) * Fixed) >> random(2)+2, ((random (100)-10) * Fixed) >> random(2)+2,
  341.                                                 random(100), MAXY, random(trailLen)+5, colour);
  342.                      }
  343.                      break;
  344.  
  345.                 case 3:
  346.                      // Circle small explosion.
  347.                      for (count = 0; count < starBurstNum; count++)
  348.                      {
  349.                         pStarBurst[count].init (x + random (10)-5,y + random (10)-5,
  350.                                                 ((random (100)-50) * Fixed) >> random(2)+2, ((random (100)-10) * Fixed) >> random(4)+2,
  351.                                                 random(100), MAXY, random(trailLen)+5, colour);
  352.                      }
  353.                      break;
  354.  
  355.                 case 4:
  356.                      // Circle small explosion.
  357.                      for (count = 0; count < starBurstNum; count++)
  358.                      {
  359.                         pStarBurst[count].init (x + random (10)-5,y + random (10)-5,
  360.                                                 ((random (20)-random(20)) * Fixed) >> random(2)+1, ((random (20)-random(20)) * Fixed) >> random(2)+1,
  361.                                                 random(100), MAXY, random(trailLen)+5, colour);
  362.                      }
  363.                      break;
  364.  
  365.                 case 5:
  366.                      // Circle small explosion.
  367.                      for (count = 0; count < starBurstNum; count++)
  368.                      {
  369.                         pStarBurst[count].init (x, y,
  370.                                                 ((random (10)-random(10)) * Fixed) >> 1, ((random (10)-random(10)) * Fixed) >> 1,
  371.                                                 random(100), MAXY, random(trailLen)+5, colour);
  372.                      }
  373.                      break;
  374.  
  375.                 case 6:
  376.                      // Downward explosion
  377.                      for (count = 0; count < starBurstNum; count++)
  378.                      {
  379.                         pStarBurst[count].init (x, y,
  380.                                                 ((random (100)-50) * Fixed) >> random(2)+2, ((random (50) - 35) * Fixed) >> random(2)+2,
  381.                                                 random(100), MAXY, random(trailLen)+5, colour);
  382.                      }
  383.                      break;
  384.  
  385.                 case 7:
  386.                      {
  387.                         // Circular explosion.
  388.                         int r;
  389.                         int xSin;
  390.                         int yCos;
  391.  
  392.                         for (count = 0; count < starBurstNum; count++)
  393.                         {
  394.                             r    = random(NUMOFDEGREES);
  395.                             xSin = (int)((SIN (r) * random(100)+10) >> SHIFT);
  396.                             yCos = (int)((COS (r) * random(100)+10) >> SHIFT);
  397.                             pStarBurst[count].init (x+xSin, y+yCos,
  398.                                                     (yCos * Fixed) >> 2, (xSin * Fixed) >> 2,
  399.                                                     random(100), MAXY, random(trailLen)+5, colour);
  400.                         }
  401.                      }
  402.                      break;
  403.  
  404.                 case 8:
  405.                      {
  406.                         // Circular explosion.
  407.                         int r;
  408.                         int xSin;
  409.                         int yCos;
  410.  
  411.                         for (count = 0; count < starBurstNum; count++)
  412.                         {
  413.                             r    = random(NUMOFDEGREES);
  414.                             xSin = (int)((SIN (r) * random(100)+10) >> SHIFT);
  415.                             yCos = (int)((COS (r) * random(100)+10) >> SHIFT);
  416.                             pStarBurst[count].init (x+xSin, y+yCos,
  417.                                                     (xSin * Fixed) >> 2, (yCos * Fixed) >> 2,
  418.                                                     random(100), MAXY, random(trailLen)+5, colour);
  419.                         }
  420.                      }
  421.                      break;
  422.  
  423.                 case 9:
  424.                      {
  425.                         // Circular explosion.
  426.                         int r;
  427.                         int xSin;
  428.                         int yCos;
  429.                         int size = random(50)+10;
  430.  
  431.                         for (count = 0; count < starBurstNum; count++)
  432.                         {
  433.                             r    = random(NUMOFDEGREES);
  434.                             xSin = (int)((SIN (r) * size) >> SHIFT);
  435.                             yCos = (int)((COS (r) * size) >> SHIFT);
  436.                             pStarBurst[count].init (x+xSin, y-yCos,
  437.                                                     (random(xSin) * Fixed) >> 2, (random(yCos) * Fixed) >> 2,
  438.                                                     random(100), MAXY, random(trailLen)+5, colour);
  439.                         }
  440.                      }
  441.                      break;
  442.             }
  443.         }
  444.     }
  445.     else // Update flare explosion.
  446.     {
  447.         int aliveCount = starBurstNum;
  448.  
  449.         // Update and text each flare in the explosion, if all dead,
  450.         // tell system.
  451.         for (int count = 0; count < starBurstNum; count++)
  452.         {
  453.              
  454.              if (pStarBurst[count].move(hdc) == FALSE)
  455.                 aliveCount--;
  456.              else
  457.              {
  458.                   COLORREF colChange = pStarBurst[count].getColour();
  459.                   int      r = GetRValue(colChange);
  460.                   int      g = GetGValue(colChange);
  461.                   int      b = GetBValue(colChange);
  462.  
  463.                   switch (colItemChange)
  464.                   {
  465.                         // Red
  466.                         case 0:
  467.                              r += colChangeAmnt;
  468.                              break;
  469.  
  470.                         // Green
  471.                         case 1:
  472.                              g += colChangeAmnt;
  473.                              break;
  474.  
  475.                         // Blue
  476.                         case 2:
  477.                              b += colChangeAmnt;
  478.                              break;
  479.                   }
  480.  
  481.                   pStarBurst[count].setColour(RGB(r, g, b));
  482.              }
  483.         }
  484.  
  485.         // Return false if all flares are DEAD.
  486.         if (aliveCount == 0)
  487.            return FALSE;
  488.     }
  489.  
  490.     return TRUE;
  491. }
  492.  
  493. // ############################################################################
  494. // ############################################################################
  495. // ############################################################################
  496.  
  497. void FlowerPot::init (int num, int x, int y, COLORREF col, int cyc, int tLen)
  498. {
  499.     if (num <= 0)
  500.        return;
  501.  
  502.     // Allocate memory.
  503.     if ((pFlares != NULL) && (num != flareNum))
  504.     {
  505.         delete pFlares;
  506.         pFlares = NULL;
  507.     }
  508.  
  509.     if (pFlares == NULL)
  510.        pFlares = new Flare[num];
  511.  
  512.     flareNum = num;
  513.     colour   = col;
  514.     cycles   = cyc;
  515.     xPos     = x;
  516.     yPos     = y;
  517.     trailLen = tLen;
  518.  
  519.  
  520.     // Select colour item to change (i.e. increment!)
  521.     // test for single primary colours
  522.     const int incFlwrAmount = 10;
  523.           int testRGB = GetRValue(colour);
  524.     testRGB += GetGValue(colour);
  525.     testRGB += GetBValue(colour);
  526.  
  527.  
  528.     if (testRGB <= 256)          // Only one colour is active
  529.     {
  530.         // Pick an primary item that isn't set!
  531.         int pick = 0;
  532.         while (pick == 0)
  533.         {
  534.             colItemChange = random (3);
  535.             switch (colItemChange)
  536.             {
  537.                 case 0:
  538.                      if (GetRValue(colour) < 127)
  539.                         pick = -1;
  540.                      break;
  541.  
  542.                 case 1:
  543.                      if (GetGValue(colour) < 127)
  544.                         pick = -1;
  545.                      break;
  546.  
  547.                 case 2:
  548.                      if (GetBValue(colour) < 127)
  549.                         pick = -1;
  550.             }
  551.         } 
  552.     }
  553.     else
  554.         colItemChange = random(3);
  555.  
  556.     // Select the increment style according to current colour value.
  557.     switch (colItemChange)
  558.     {
  559.          // red
  560.          case 0:
  561.               if (GetRValue(colour) < 127)
  562.                  colChangeAmnt = incFlwrAmount;
  563.               else
  564.                  colChangeAmnt = -incFlwrAmount;
  565.               break;
  566.  
  567.          // green
  568.          case 1:
  569.               if (GetGValue(colour) < 127)
  570.                  colChangeAmnt = incFlwrAmount;
  571.               else
  572.                  colChangeAmnt = -incFlwrAmount;
  573.               break;
  574.  
  575.  
  576.          // blue
  577.          case 2:
  578.              if (GetBValue(colour) < 127)
  579.                 colChangeAmnt = incFlwrAmount;
  580.              else
  581.                 colChangeAmnt = -incFlwrAmount;
  582.              break;
  583.     }
  584.  
  585.  
  586.     // Setup inital flares !
  587.     if (pFlares == NULL)
  588.         return;
  589.  
  590.     for (int count = 0; count < flareNum; count++)
  591.     {
  592.          pFlares[count].init (xPos, yPos,
  593.                               ((random (10)-5) * Fixed) >> random(3)+1, (random (20) * Fixed) >> random(2)+1,
  594.                               random(50), yPos, trailLen, colour);
  595.     }
  596. }
  597.  
  598. BOOL FlowerPot::move (HDC hdc)
  599. {
  600.     // If noting allocated, nothing to move.
  601.     if (pFlares == NULL)
  602.        return FALSE;
  603.  
  604.     if (cycles > 0)
  605.     {
  606.         cycles--;
  607.         for (int count = 0; count < flareNum; count++)
  608.         {
  609.             // Update flares
  610.             if (pFlares[count].move(hdc) == FALSE)
  611.             {
  612.                 pFlares[count].init (xPos, yPos,
  613.                                      ((random (10)-5) * Fixed) >> random(3)+1,
  614.                                      (random (20) * Fixed) >> random(2)+1,
  615.                                      random(50), yPos, trailLen, colour);
  616.             }
  617.             else
  618.             {
  619.                   COLORREF colChange = pFlares[count].getColour();
  620.                   int      r = GetRValue(colChange);
  621.                   int      g = GetGValue(colChange);
  622.                   int      b = GetBValue(colChange);
  623.  
  624.                   switch (colItemChange)
  625.                   {
  626.                         // Red
  627.                         case 0:
  628.                              r += colChangeAmnt;
  629.                              break;
  630.  
  631.                         // Green
  632.                         case 1:
  633.                              g += colChangeAmnt;
  634.                              break;
  635.  
  636.                         // Blue
  637.                         case 2:
  638.                              b += colChangeAmnt;
  639.                              break;
  640.                   }
  641.  
  642.                   pFlares[count].setColour(RGB(r, g, b));
  643.              }
  644.         }
  645.     }
  646.     else
  647.     {
  648.         int testAlive = 0;
  649.  
  650.         for (int count = 0; count < flareNum; count++)
  651.         {
  652.             if (pFlares[count].move(hdc) == FALSE)
  653.                    testAlive++;
  654.             else
  655.             {
  656.                 // Implement random intensity of colour change.
  657.                 // set intensity difference;
  658.                 int intChange = 15;
  659.                 COLORREF colChange = pFlares[count].getColour();
  660.                 int      r = GetRValue(colChange);
  661.                 int      g = GetGValue(colChange);
  662.                 int      b = GetBValue(colChange);
  663.  
  664.                 // Determine upward or downward change
  665.                 if (random(2))
  666.                 {
  667.                     // Upward change
  668.                     if ((r += intChange) > 255)
  669.                        r -= intChange;
  670.  
  671.                     if ((g += intChange) > 255)
  672.                        g -= intChange;
  673.  
  674.                     if ((b += intChange) > 255)
  675.                        b -= intChange;
  676.                 }
  677.                 else
  678.                 {
  679.                     // Downward change
  680.                     if ((r -= intChange) < 0)
  681.                        r += intChange;
  682.  
  683.                     if ((g -= intChange) < 0)
  684.                        g += intChange;
  685.  
  686.                     if ((b -= intChange) < 0)
  687.                        b += intChange;
  688.                 }
  689.  
  690.                 pFlares[count].setColour(RGB(r, g, b));
  691.             }
  692.         }
  693.  
  694.         // Test if flares are still active.
  695.         if (testAlive == flareNum)
  696.             return FALSE;
  697.         else
  698.             return TRUE;
  699.     }
  700.  
  701.     return TRUE;
  702. }
  703.  
  704. // ############################################################################
  705. // ############################################################################
  706. // ############################################################################